package com;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpGlobalConfig;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import org.apache.commons.lang3.ObjectUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import us.codecraft.xsoup.Xsoup;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Python3 {

    private final static Log log = Log.get();

    static Set<String> set = new HashSet<>();
    static String url = "https://live.douyin.com/webcast/room/web/enter/?web_rid=%s&aid=6383&app_name=douyin_web&live_id=1&device_platform=web&language=zh-CN&enter_from=web_live&cookie_enabled=true&screen_width=1920&screen_height=1080&browser_language=zh-CN&browser_platform=Win32&browser_name=Chrome&browser_version=114.0.0.0&room_id_str=7249245966065240887&enter_source=&Room-Enter-User-Login-Ab=0&is_need_double_stream=false&msToken=smzk2Ma7_S6j3UysYnIz7AZI069ELebLaC73zHAxujYvBNuwa_3kn2skQS5s1RTkaXJRTD0tBfWLcjVfz46R3SEZp1sSinYtqrv1uleXnKZfRhZW-lL-&X-Bogus=DFSzswVYpqTANx/PtnScJM9WX7nC";
    static String Cookie = "ttwid=1%7CjEvB7s-vakY3d2vkBl_qpjpXPjshGqybp8rSLSjCbBA%7C1687943892%7Cf89e0d24ba5f04a50ac71374bcb5a719f9222c762dc723e938ba47366726dcf8; strategyABtestKey=%221687943894.328%22; passport_csrf_token=4edb4b5ff7be4e107ac308218ebd71ea; passport_csrf_token_default=4edb4b5ff7be4e107ac308218ebd71ea; FORCE_LOGIN=%7B%22videoConsumedRemainSeconds%22%3A180%7D; home_can_add_dy_2_desktop=%221%22; pwa2=%220%7C0%7C1%7C0%22; __live_version__=%221.1.1.890%22; device_web_cpu_core=6; device_web_memory_size=8; csrf_session_id=dfc21ce9d976cfda0a2c495c779c93e3; xgplayer_user_id=295796351897; ttcid=930a7d5425594c9eb71c4c8f8e16afc028; tt_scid=eTOCxiKxHgtWRtyI1qlfzXptHJvPQ-7cq0ONDIzL.lqQGBAfh3z9UJ9d6OImhBYE9159; __ac_nonce=0649bfe9e00b412695b08; __ac_signature=_02B4Z6wo00f01bbuJjwAAIDBNuzcf.0jqCW2ziKAAAkhQ7GoTdSYXIFqY.x0ut2DePxWS-DIN5YlIciopjZyYRBWobcOd.yEOsrSX6RS7ymmsWhehUiLm6KzcuYqJqbRIy8qO.YeX3SfTxvbe5; webcast_local_quality=ld; odin_tt=f8de98d0df3c24455a6ff5f2cf6aa6dfbbf10f62cafd8fadb5bcce88da8a3d7ebb3a0fbd57085cd9e633cc254a27f614506ee1ca4b5768f56dc7ceb98aaaa428ea459e046cda2caa30906da2e1a2366f; webcast_leading_last_show_time=1687944864952; webcast_leading_total_show_times=1; msToken=gz0YF-os5XABlGZ7fmexwex-64CARWEewslYAL6Ve9Xj4ZWuQdQ1WwLnuSsx6-6UcDDtL1sexnpI-o-40rVQdyOsLsMq1oN1jMq9iUSe5xncaXr9cqp2PhDCMhhKzQ==; msToken=smzk2Ma7_S6j3UysYnIz7AZI069ELebLaC73zHAxujYvBNuwa_3kn2skQS5s1RTkaXJRTD0tBfWLcjVfz46R3SEZp1sSinYtqrv1uleXnKZfRhZW-lL-; download_guide=%223%2F20230628%2F0%22; live_can_add_dy_2_desktop=%220%22";
    private final static ThreadPoolExecutor executorMiddleService = new ThreadPoolExecutor(
            2, 3,
            60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());

    public static void main(String[] args) throws InterruptedException {
//        System.out.println(HttpUtil.get(" http://youke.cn-idvhliy.qiniudns.com.zhaowho.cn/qq.php?t=163801067875232"));
        System.out.println(set.add("1"));
        System.out.println(set.add("1"));
        System.out.println(set.add("1"));
        String id = "880330956591";

//        String url = "http://wuci.cn-idvhliy.qinfttg.cn.zhaowho.cn/qq.php?t=163801067875232";
        http(String.format(url, id), id, true);

    }

    public static void http(String soap, String id, Boolean bool) throws InterruptedException {
        String fenge = "#,,,#";
        String str = HttpRequest.get(soap).header("Cookie", Cookie).timeout(-1).execute().body();;
        final JSONObject jsonObject = JSONUtil.parseObj(str);
        final JSONObject data = jsonObject.getJSONObject("data");
        final JSONArray userDataArr = data.getJSONArray("data");
        final JSONObject userData = userDataArr.getJSONObject(0);
        System.out.print(id + fenge);
        System.out.print(userData.getStr("title") + fenge);
        final String nickname = userData.getJSONObject("owner").getStr("nickname");
        System.out.print(nickname + fenge);

        final JSONObject roomViewStats = userData.getJSONObject("room_view_stats");
        if (roomViewStats != null) {
            System.out.print(roomViewStats.getStr("display_value") + fenge);
        } else {
            System.out.print(0 + fenge);
        }
        System.out.println();
        final JSONArray similarRooms = data.getJSONArray("similar_rooms");
        for (Object similarRoom : similarRooms) {
            similarRoom = (JSONObject)similarRoom;
            final String webRid = ((JSONObject) similarRoom).getStr("web_rid");
            if (set.add(webRid)) {
                Thread.sleep(50);
                http(String.format(url, webRid), webRid, true);
            }
        }
    }

    public static  List<String>  test(String item){
        Pattern p=Pattern.compile("src=\"(.*?)0\"");
        Matcher m=p.matcher(item);
        List<String> urls = new ArrayList<>();
        while(m.find()){
            urls.add(m.group(1) + "0");
        }
        return urls;
    }
}
